﻿//参考@被遗忘的青色剑士 科技版MOD
//由@不勤奋的儒雅 提供部分注释解析
//作者:我不搞直播了
//已经在数据库注册的行为或组件要在前面标明//已注册;注册报错
//由销锋镝铸提供技术指导
using Engine;
using Game;
using Engine.Graphics;

namespace Mod
{
    //未测试   空气墙方块
    public class AirWallBlock : AlphaTestCubeBlock
    {
        public static int Index = 700;

        public AirWallBlock()
        {
            this.DefaultCreativeData = -1;
            this.IsTransparent = true;
            this.IsDiggingTransparent = true;
            this.DigResilience = float.PositiveInfinity;
        }

        public static bool ShouldGenerateFace( SubsystemTerrain subsystemTerrain, int face, int value,int neighborValue)
        {
            return false;
        }

        public override float GetProjectileResilience(int value) => float.PositiveInfinity;

        public override float GetExplosionResilience(int value) => float.PositiveInfinity;
    }
    
    public class Cotton_BetterBlock : Block

    {
        public static int Index = 701;

        public Texture2D ?m_texture;

        public override void Initialize()
        {
            base.Initialize();
            m_texture = ContentManager.Get<Texture2D>("Textures/NewBlock/Cotton_BetterBlock");
        }

        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
        }

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            BlocksManager.DrawFlatBlock(primitivesRenderer, value, 1f * size, ref matrix, m_texture, Color.White, isEmissive: true, environmentData);
        }
    }
    
    public class Coal_BetterBlock : Block
    {
        public static int Index = 702;

        private BlockMesh m_standaloneBlockMesh = new BlockMesh();
        public Texture2D texture = ContentManager.Get<Texture2D>("Textures/NewBlock/Coal_BetterBlock");
        public override void Initialize()
        {
            Model model = ContentManager.Get<Model>("Models/Chunk");
            Matrix boneAbsoluteTransform1 = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("GeoSphere002").ParentBone);
            m_standaloneBlockMesh.AppendModelMeshPart(model.FindMesh("GeoSphere002").MeshParts[0], boneAbsoluteTransform1 * Matrix.CreateTranslation(0f, -0.1f, 0f) * Matrix.CreateRotationX(1.5707f), false, false, true, false, Color.Black);
            base.Initialize();
        }
        //放在地上的时候的渲染
        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
            generator.GenerateShadedMeshVertices(this, x, y, z, m_standaloneBlockMesh, Color.Black, null, null, geometry.SubsetOpaque);
        }
        //在手上拿着的时候的渲染
        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            BlocksManager.DrawMeshBlock(primitivesRenderer, m_standaloneBlockMesh, texture, color, 1f * size, ref matrix, environmentData);
        }
    }
    public class LuckyBlock : CubeBlock
    {
        public static int Index = 703;

        public LuckyBlock()
        {
            this.IsTransparent = true;
            this.DefaultShadowStrength = -1;
            this.DefaultDropContent = 300;
            this.DefaultDropCount = 1f;
            this.DefaultCategory = "Items";
            this.DisplayOrder = 300;
            this.DefaultDisplayName = "幸运方块";
            this.DefaultDescription = "普通幸运方块，可以随机获得物品或一些不好的事件";
        }

        public override int GetFaceTextureSlot(int face, int value) => 180;
    }
    
    public class VeryLuckyBlock : CubeBlock
    {
        public static int Index = 704;

        public VeryLuckyBlock()
        {
            this.IsTransparent = true;
            this.DefaultShadowStrength = -1;
            this.DefaultDropContent = 300;
            this.DefaultDropCount = 1f;
            this.DefaultCategory = "Items";
            this.DisplayOrder = 300;
            this.DefaultDisplayName = "超级幸运方块";
            this.DefaultDescription = "超级幸运的方块，可以随机获得高级物品或武器";
        }

        public override int GetFaceTextureSlot(int face, int value) => 180;
    }
    
    //继承用方块
    //可以直接用的箱子
    public class bigChestBlock : CubeBlock
    {
        public static int Index = 705;

        public override int GetFaceTextureSlot(int face, int value)
        {
            if (face == 4 || face == 5)
                return 42;
            switch (Terrain.ExtractData(value))
            {
                case 0:
                    return face == 0 || face == 2 ? 4 : 4;
                case 1:
                    return face == 1 || face == 3 ? 4 : 4;
                case 2:
                    if (face == 0)
                        return 4;
                    return 4;
                default:
                    if (face == 1)
                        return 26;
                    return face == 3 ? 27 : 25;
            }
        }

        public override BlockPlacementData GetPlacementValue(
            SubsystemTerrain subsystemTerrain, 
            ComponentMiner componentMiner, 
            int value, 
            TerrainRaycastResult raycastResult)
        {
            Vector3 forward = Matrix.CreateFromQuaternion(componentMiner.ComponentCreature.ComponentCreatureModel.EyeRotation).Forward;
            float x1 = Vector3.Dot(forward, Vector3.UnitZ);
            float x2 = Vector3.Dot(forward, Vector3.UnitX);
            float x3 = Vector3.Dot(forward, -Vector3.UnitZ);
            float x4 = Vector3.Dot(forward, -Vector3.UnitX);
            int data = 0;
            if ((double)x1 == (double)MathUtils.Max(x1, x2, x3, x4))
                data = 2;
            else if ((double)x2 == (double)MathUtils.Max(x1, x2, x3, x4))
                data = 3;
            else if ((double)x3 == (double)MathUtils.Max(x1, x2, x3, x4))
                data = 0;
            else if ((double)x4 == (double)MathUtils.Max(x1, x2, x3, x4))
                data = 1;
            return new BlockPlacementData()
            {
                Value = Terrain.ReplaceData(Terrain.ReplaceContents(0, 45), data),
                CellFace = raycastResult.CellFace
            };
        }
    }
    //测试方块，无实际用途
    public class DigBoxBlock : Block
    {
        public static int Index = 706;
        public override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List<BlockDropValue> dropValues, out bool showDebris)
        {
            int num = Terrain.ExtractData(oldValue);
            int num2 = 10 + (4 * num);
            int num3 = (num >= 3) ? 1 : 0;
            BlockDropValue item;
            for (int i = 0; i < 3; i++)
            {
                item = new BlockDropValue
                {
                    Value = 79,
                    Count = 1
                };
                dropValues.Add(item);
            }
            for (int j = 0; j < num2; j++)
            {
                item = new BlockDropValue
                {
                    Value = 248,
                    Count = 1
                };
                dropValues.Add(item);
            }
            for (int k = 0; k < num3; k++)
            {
                item = new BlockDropValue
                {
                    Value = 111,
                    Count = 1
                };
                dropValues.Add(item);
            }
            for (int l = 0; l < 2; l++)
            {
                item = new BlockDropValue
                {
                    Value = 23,
                    Count = 1
                };
                dropValues.Add(item);
            }
            showDebris = false;
        }
        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {

        }

        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {

        }
    }
}






//新的空气墙
/*
public class AirWallBlock : AlphaTestCubeBlock
{
    public const int Index = 707;

    //空气墙方块
    public AirWallBlock()
    {
        DefaultCreativeData = -1;
    }

    public override bool ShouldGenerateFace(SubsystemTerrain subsystemTerrain, int face, int value, int neighborValue)
    {
        return false;
    }

    public override float GetDigResilience(int value)
    {
        return float.PositiveInfinity;
    }

    public override float GetProjectileResilience(int value)
    {
        return float.PositiveInfinity;
    }

    public override float GetExplosionResilience(int value)
    {
        return float.PositiveInfinity;
    }
}
*/

namespace SCTemplate
{
    public class Test : Block
    {
        public const int Index = 1011;
        public List<Texture2D> textures = new System.Collections.Generic.List<Texture2D>();
        public override void Initialize()
        {
            //动态贴图
            ReadOnlyList<ContentInfo> contentInfos = ContentManager.List("images");//直接把所有贴图加载到textures里面，路径：Assets/images
            foreach (var c in contentInfos)
            {
                textures.Add(ContentManager.Get<Texture2D>(c.ContentPath));
            }

        }
        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
        }
        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            BlocksManager.DrawFlatBlock(primitivesRenderer,value,size,ref matrix,textures[Time.FrameIndex%textures.Count],Color.White,false,environmentData);
        }
    }

}
